{
  "name": "Customer Contract Amendment Request Agent",
  "nodes": [
    {
      "parameters": {
        "updates": ["message"]
      },
      "id": "tg-trigger-17",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "name": "user_message",
              "value": "={{ $json.message.text }}",
              "type": "string"
            },
            {
              "name": "user_name",
              "value": "={{ $json.message.from.first_name + ' ' + ($json.message.from.last_name || '') }}",
              "type": "string"
            },
            {
              "name": "chat_id",
              "value": "={{ $json.message.chat.id }}",
              "type": "string"
            }
          ]
        }
      },
      "id": "set-context-17",
      "name": "Workflow Configuration",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [220, 0]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=User Name: {{ $json.user_name }}\nUser Message: {{ $json.user_message }}\nChat ID: {{ $json.chat_id }}",
        "options": {
          "systemMessage": "=You are a Customer Contract Amendment Request Assistant.\n\nYour responsibilities:\n1. Identify whether the user wants to REGISTER a new contract amendment request or CHECK the status of an existing amendment.\n\n2. For new amendment requests, collect:\n   - Contract ID\n   - Amendment Type (Pricing, Scope Change, Payment Terms, Term Extension, Clause Modification)\n   - Brief description of the requested amendment\n\n3. Validate the contract using the Contract Master Tool and extract:\n   - Customer_Name\n   - Contract_Status\n   - Account_Manager_Email (used for email communication)\n\n4. If the contract is valid and active:\n   - Generate a unique amendment reference ID in the format:\n     AMD-YYYYMMDD-[last 4 digits of contract]-HHmmss\n   - Register the amendment using the Contract Amendment Register Tool\n   - Send an acknowledgement email using the Gmail Tool to Account_Manager_Email\n   - Clearly explain legal/commercial review workflow and expected timelines\n   - Respond using the Telegram Response Tool\n\n5. If the contract is invalid or inactive:\n   - Politely explain the reason\n   - Do NOT register the amendment\n   - Respond using the Telegram Response Tool\n\n6. For status checks:\n   - Ask for amendment reference ID if missing\n   - Retrieve current status using the Contract Amendment Status Tool\n   - Explain the current review stage and next steps\n   - Respond using the Telegram Response Tool\n\nStrict rules:\n- Do NOT promise approval or timelines.\n- Do NOT provide legal advice.\n- Account manager email must be retrieved from the master sheet only.\n- ALWAYS use the Telegram Response Tool."
        }
      },
      "id": "agent-17",
      "name": "Contract Amendment Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3,
      "position": [500, 0]
    },
    {
      "parameters": {
        "model": { "mode": "id", "value": "gpt-4o" },
        "options": { "temperature": 0.2 }
      },
      "id": "llm-17",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [320, 220]
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Workflow Configuration').item.json.chat_id }}"
      },
      "id": "memory-17",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [500, 220]
    },
    {
      "parameters": {
        "descriptionType": "manual"
      },
      "id": "contract-master-17",
      "name": "Contract Master Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [640, 260]
    },
    {
      "parameters": {
        "operation": "append",
        "descriptionType": "manual"
      },
      "id": "contract-register-17",
      "name": "Contract Amendment Register Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [800, 260]
    },
    {
      "parameters": {
        "descriptionType": "manual"
      },
      "id": "contract-status-17",
      "name": "Contract Amendment Status Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [960, 320]
    },
    {
      "parameters": {
        "sendTo": "={{ $fromAI('account_manager_email') }}",
        "subject": "={{ $fromAI('email_subject') }}",
        "message": "={{ $fromAI('email_body') }}"
      },
      "id": "gmail-17",
      "name": "Contract Amendment Notification Email",
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.1,
      "position": [1120, 220]
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.output }}"
      },
      "id": "tg-response-17",
      "name": "Send Telegram Response",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [960, 0]
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [[{ "node": "Workflow Configuration", "type": "main" }]]
    },
    "Workflow Configuration": {
      "main": [[{ "node": "Contract Amendment Agent", "type": "main" }]]
    },
    "Contract Amendment Agent": {
      "main": [[{ "node": "Send Telegram Response", "type": "main" }]]
    }
  },
  "active": false
}
